package com.tanhua.server.controller;

import com.tanhua.model.domain.PageResult;
import com.tanhua.model.domain.mongo.Movement;
import com.tanhua.model.domain.vo.MovementsVo;
import com.tanhua.model.domain.vo.VisitorsVo;
import com.tanhua.server.service.CommentService;
import com.tanhua.server.service.MovementsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author lk
 * @date 2022-03-26
 */
@RestController
@RequestMapping("/movements")
public class MovementsControlle {
    @Autowired
    private MovementsService movementsService;
    @Autowired
    private CommentService commentService;


    /**
     * 发布动态
     */
    @PostMapping         //movement  动态内容       imageContent图片或视频
    public ResponseEntity movements(Movement movement, MultipartFile imageContent[]) throws IOException {
        movementsService.publishMovement(movement, imageContent);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询我的动态
     */
    @GetMapping("/all")
    public ResponseEntity findByUserId(Long userId,
                                       @RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer pageSize) {
        PageResult pageResult = movementsService.findByUserId(userId, page, pageSize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 查询好友动态
     */
    @GetMapping
    public ResponseEntity movements(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        PageResult pageResult = movementsService.findFriendMovements(page, pageSize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 推荐动态
     */
    @GetMapping("/recommend")
    public ResponseEntity recommend(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = movementsService.findRecommendMovements(page,pagesize);
        return ResponseEntity.ok(pageResult);
    }


    //点赞
    @GetMapping("/{id}/like")
    public ResponseEntity like (@PathVariable("id") String movementId){
        Integer likeCount = commentService.likeComment(movementId);
        return ResponseEntity.ok(likeCount);
    }

    //取消点赞
    @GetMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String movementId) {
        Integer likeCount = commentService.dislikeComment(movementId);
        return ResponseEntity.ok(likeCount);
    }

    //喜欢
    @GetMapping("/{id}/love")
    public ResponseEntity love(@PathVariable("id") String movementId) {
        Integer loveCount = commentService.loveComment(movementId);
        return ResponseEntity.ok(loveCount);
    }

    //取消喜欢
    @GetMapping("/{id}/unlove")
    public ResponseEntity unlove(@PathVariable("id") String movementId) {
        Integer loveCount = commentService.disloveComment(movementId);
        return ResponseEntity.ok(loveCount);
    }

    //查看单条动态
    @GetMapping("/{id}")
    public ResponseEntity findById(@PathVariable("id") String movementId){
        MovementsVo vo = movementsService.findMovement(movementId);
        return ResponseEntity.ok(vo);
    }

    //谁看过我(访客记录)
    @GetMapping("/visitors")
    public ResponseEntity queryVisitorsList(){
        List<VisitorsVo> list = movementsService.queryVisitorsList();
        return ResponseEntity.ok(list);
    }
}
